package org.convallaria.system.biz.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 性能监控服务接口
 * 
 * @author convallaria
 * @since 1.0.0
 */
public interface PerformanceMonitorService {

    /**
     * 记录接口性能
     * 
     * @param method 方法名
     * @param duration 执行时间（毫秒）
     * @param success 是否成功
     */
    void recordApiPerformance(String method, long duration, boolean success);

    /**
     * 记录数据库查询性能
     * 
     * @param sql SQL语句
     * @param duration 执行时间（毫秒）
     * @param success 是否成功
     */
    void recordDatabasePerformance(String sql, long duration, boolean success);

    /**
     * 记录缓存性能
     * 
     * @param operation 操作类型（GET/SET/DELETE）
     * @param key 缓存键
     * @param duration 执行时间（毫秒）
     * @param hit 是否命中
     */
    void recordCachePerformance(String operation, String key, long duration, boolean hit);

    /**
     * 获取性能统计
     * 
     * @param timeRange 时间范围（小时）
     * @return 性能统计
     */
    PerformanceStatistics getPerformanceStatistics(int timeRange);

    /**
     * 获取慢接口列表
     * 
     * @param limit 限制数量
     * @return 慢接口列表
     */
    List<SlowOperation> getSlowOperations(int limit);

    /**
     * 获取系统资源使用情况
     * 
     * @return 资源使用情况
     */
    SystemResourceUsage getSystemResourceUsage();

    /**
     * 清理过期性能数据
     */
    void cleanExpiredData();

    /**
     * 性能统计信息
     */
    class PerformanceStatistics {
        private long totalRequests;
        private long successfulRequests;
        private long failedRequests;
        private double averageResponseTime;
        private double p95ResponseTime;
        private double p99ResponseTime;
        private long slowOperations;
        private LocalDateTime timestamp;

        public PerformanceStatistics(long totalRequests, long successfulRequests, long failedRequests,
                                   double averageResponseTime, double p95ResponseTime, double p99ResponseTime,
                                   long slowOperations, LocalDateTime timestamp) {
            this.totalRequests = totalRequests;
            this.successfulRequests = successfulRequests;
            this.failedRequests = failedRequests;
            this.averageResponseTime = averageResponseTime;
            this.p95ResponseTime = p95ResponseTime;
            this.p99ResponseTime = p99ResponseTime;
            this.slowOperations = slowOperations;
            this.timestamp = timestamp;
        }

        // Getters
        public long getTotalRequests() { return totalRequests; }
        public long getSuccessfulRequests() { return successfulRequests; }
        public long getFailedRequests() { return failedRequests; }
        public double getAverageResponseTime() { return averageResponseTime; }
        public double getP95ResponseTime() { return p95ResponseTime; }
        public double getP99ResponseTime() { return p99ResponseTime; }
        public long getSlowOperations() { return slowOperations; }
        public LocalDateTime getTimestamp() { return timestamp; }
        
        public double getSuccessRate() {
            return totalRequests > 0 ? (double) successfulRequests / totalRequests : 0.0;
        }
    }

    /**
     * 慢操作信息
     */
    class SlowOperation {
        private String operation;
        private String type; // API/DB/CACHE
        private long duration;
        private LocalDateTime timestamp;
        private String details;

        public SlowOperation(String operation, String type, long duration, LocalDateTime timestamp, String details) {
            this.operation = operation;
            this.type = type;
            this.duration = duration;
            this.timestamp = timestamp;
            this.details = details;
        }

        // Getters
        public String getOperation() { return operation; }
        public String getType() { return type; }
        public long getDuration() { return duration; }
        public LocalDateTime getTimestamp() { return timestamp; }
        public String getDetails() { return details; }
    }

    /**
     * 系统资源使用情况
     */
    class SystemResourceUsage {
        private double cpuUsage;
        private long memoryUsed;
        private long memoryTotal;
        private long diskUsed;
        private long diskTotal;
        private int activeConnections;
        private int maxConnections;
        private LocalDateTime timestamp;

        public SystemResourceUsage(double cpuUsage, long memoryUsed, long memoryTotal,
                                 long diskUsed, long diskTotal, int activeConnections,
                                 int maxConnections, LocalDateTime timestamp) {
            this.cpuUsage = cpuUsage;
            this.memoryUsed = memoryUsed;
            this.memoryTotal = memoryTotal;
            this.diskUsed = diskUsed;
            this.diskTotal = diskTotal;
            this.activeConnections = activeConnections;
            this.maxConnections = maxConnections;
            this.timestamp = timestamp;
        }

        // Getters
        public double getCpuUsage() { return cpuUsage; }
        public long getMemoryUsed() { return memoryUsed; }
        public long getMemoryTotal() { return memoryTotal; }
        public long getDiskUsed() { return diskUsed; }
        public long getDiskTotal() { return diskTotal; }
        public int getActiveConnections() { return activeConnections; }
        public int getMaxConnections() { return maxConnections; }
        public LocalDateTime getTimestamp() { return timestamp; }
        
        public double getMemoryUsagePercent() {
            return memoryTotal > 0 ? (double) memoryUsed / memoryTotal * 100 : 0.0;
        }
        
        public double getDiskUsagePercent() {
            return diskTotal > 0 ? (double) diskUsed / diskTotal * 100 : 0.0;
        }
        
        public double getConnectionUsagePercent() {
            return maxConnections > 0 ? (double) activeConnections / maxConnections * 100 : 0.0;
        }
    }
}
